package cn.iocoder.yudao.module.vos.dal.mysql.marketingscore;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.vos.controller.app.marketingscore.vo.MarketingScoreApiSqlProvider;
import cn.iocoder.yudao.module.vos.dal.dataobject.marketingscore.MarketingScoreDO;
import cn.iocoder.yudao.module.vos.job.vo.PhoneScoreDO;
import org.apache.ibatis.annotations.Lang;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.vos.controller.admin.marketingscore.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.scripting.xmltags.XMLLanguageDriver;

/**
 * 名单评分 Mapper
 *
 * @author shijun
 */
@Mapper
public interface MarketingScoreMapper extends BaseMapperX<MarketingScoreDO> {

    /**
     * 便捷分页封装（分表版）
     * 计算 offset/limit -> 走分表 SQL（UNION ALL）-> 再 count
     */
    default PageResult<MarketingScoreDO> selectPage(MarketingScorePageReqVO reqVO) {
        int pageNo = reqVO.getPageNo() == null ? 1 : reqVO.getPageNo();
        int pageSize = reqVO.getPageSize() == null ? 10 : reqVO.getPageSize();
        int offset = (pageNo - 1) * pageSize;

        List<MarketingScoreDO> list = selectPageByShards(reqVO, offset, pageSize);
        Long total = countByShards(reqVO);
        return new PageResult<>(list, total);
    }

    @SelectProvider(type = MarketingScoreSqlProvider.class, method = "buildSelectPageScript")
    @Lang(XMLLanguageDriver.class)
    List<MarketingScoreDO> selectPageByShards(@Param("req") MarketingScorePageReqVO reqVO,
                                              @Param("offset") int offset,
                                              @Param("limit") int limit);

    @SelectProvider(type = MarketingScoreSqlProvider.class, method = "buildCountScript")
    @Lang(XMLLanguageDriver.class)
    Long countByShards(@Param("req") MarketingScorePageReqVO reqVO);

    Integer checkTableExists(@Param("table") String table);

    int createScoreTable(@Param("table") String table);

    Long countAll(@Param("table") String table);

    int insertScoresToTable(@Param("table") String table,
                            @Param("items") List<PhoneScoreDO> items);

    List<PhoneScoreDO> pageByTable(@Param("table") String table,
                                   @Param("limit") int limit,
                                   @Param("offset") int offset);

    int truncateTable(@Param("table") String table); // 可选：重建时用



    /** 在“当日Top-N（score DESC）”集合中分页，仅返回 phone_md5 */
    @SelectProvider(type = MarketingScoreApiSqlProvider.class, method = "buildSelectTopPhoneMd5Page")
    @Lang(XMLLanguageDriver.class)
    List<String> selectTopPhoneMd5Page(@Param("table") String table,
                                       @Param("quota") int quota,
                                       @Param("offset") int offset,
                                       @Param("limit") int limit);

    /** 统计“当日Top-N”集合的总数（= min(quota, 实际条数)） */
    @SelectProvider(type = MarketingScoreApiSqlProvider.class, method = "buildCountTop")
    @Lang(XMLLanguageDriver.class)
    Long countTop(@Param("table") String table,
                  @Param("quota") int quota);

    // —— 新增：从“分表”按分数降序获取某等级 TopN —— //
    List<MarketingScoreDO> selectTopByLevelFromTable(@Param("table") String table,
                                                     @Param("level") String level,
                                                     @Param("limit") int limit);
}